 
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/custom_widget.dart';
import 'package:osg/controllers/http.dart';
import '/kong.dart' if (dart.library.io) 'package:osg/middleware/im_manager.dart';

// import 'package:tencent_cloud_chat_sdk/enum/friend_type_enum.dart';
 

class AddChat extends StatefulWidget {
  const AddChat({super.key});

  @override
  State<AddChat> createState() => _SettingPageState();
}


class _SettingPageState extends State<AddChat> {

  late TextEditingController searchController;

    // TextEditingController searchController = TextEditingController(text: 'OSG100000393');

    RxString nickName = ''.obs;
    RxString faceUrl = ''.obs;
    RxString userID = ''.obs;
    RxString tvalue = ''.obs;
    RxBool isback=false.obs;

    @override
    void initState() {
      super.initState();
     searchController = TextEditingController(text: '');
      
    }

 
  void _search(String keyword) async{


      // final results  =  await ImManager().checkFriend(userIDList: [searchController.text], checkType:FriendTypeEnum.V2TIM_FRIEND_TYPE_SINGLE);
      //   if(results.code == 0){
      //      print(8888888);

      //     print(results.data?.first.resultType)
      //     ;
      //   }else{
      //      print(66666);
      //      print(results.code);
      //       print(results.data?.first.resultType)
      //     ;
      //   }
                 

    // 在这里实现搜索逻辑，例如打印关键词


    print('搜索关键词: $keyword');

    // 可以在这里调用API或过滤数据等
      final response =  await ImManager().getFriendsInfo(userId:searchController.text);
      final info = response?.friendInfo?.userProfile;
      nickName.value = info?.nickName ?? '';
      faceUrl.value = info?.faceUrl ?? '';
      userID.value = info?.userID ?? '';


                    

  }

   @override
  void dispose() {
    searchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final totalHeight = MediaQuery.of(context).padding.top + 15;

    return Scaffold(
        body: Stack(children: [
      ListView(
        padding: EdgeInsets.only(top: totalHeight + 35, left: 15, right: 15),
        children: [
          Stack(
            children: [
              Container(
                alignment: Alignment.center,
                height: 40,
                child: TextField(
                  controller: searchController,
                  keyboardType: TextInputType.text,
                  onChanged: (value) {
                    tvalue.value = value;
                  },
                   textInputAction: TextInputAction.search,
                    onSubmitted: (value) {
                    _search(value);
                  },
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black, // 文本颜色
                    fontSize: 12.0, // 字体大小
                  ),
                  decoration: InputDecoration(
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                          color: Colors.white, width: 0), // 激活状态下的边框颜色和宽度
                      borderRadius: BorderRadius.circular(10.0), // 设置圆角边框半径
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                          color: Colors.white, width: 0), // 激活状态下的边框颜色和宽度
                      borderRadius: BorderRadius.circular(10.0), // 设置圆角边框半径
                    ),
                    filled: true,
                    fillColor: Color(0xfff5f5f5),
                    hintText: '搜索',
                    hintStyle:
                        TextStyle(fontSize: 12, color: Color(0xff999999)),
                    border: InputBorder.none,
                  ),
                ),
              ),
              Obx(()=>
               tvalue.isEmpty?
              Positioned(
                top: 11,
                // 第一个层叠的小部件
                left: -40,
                right: 0,
                child: Icon(Icons.search, size: 15, color: Color(0xff999999)),
              ):SizedBox()
              )
            ],
          ),

          Obx(()=>
          nickName.isNotEmpty?
          Container(
            padding: EdgeInsets.only(bottom: 15, top: 15),
            child: Row(
              children: [
                CircleAvatar(
                    radius: 16, backgroundImage: NetworkImage('$faceUrl')),
                SizedBox(
                  width: 10,
                ),
                
                 Text(
                  '$nickName',
                  style: TextStyle(fontSize: 14, color: Color(0XFF333333)),
                
                )
               ,
                Expanded(child: SizedBox()),
                GestureDetector(
                  onTap: () async{

                    

                      final result = await ImManager().addFriend(
                        userId: searchController.text,
                        remark: '',
                      );
                      if(result){
                        Get.find<HTTPS>().showTost('添加成功');
                        isback.value=true;
                      }
                      
                  },
                  child: Container(
                  child: Text('添加',style: TextStyle(fontSize: 14,color: Color(0XFF333333))),
                ),
                )
                
              ],
            ),
          ):isdata()
          )
          ,
          
        ],
      ),
      Positioned(
        child: Container(
          height: 50,
          alignment: Alignment.topCenter,
          color: Color(0XFFFFFFFF),
          child: Image.asset(
            'static/images/hometop.png',
            fit: BoxFit.fitWidth,
            width: double.infinity,
          ),
        ),
      ),
      Positioned(
          top: totalHeight,
          left: 0,
          right: 0,
          child: Align(
            child: Text('添加朋友',
                style: TextStyle(fontSize: 16, color: Color(0XFF333333))),
          )),
      Positioned(
          top: totalHeight,
          left: 15,
          child: GestureDetector(
              onTap: () {
                if(isback.value){
                  Get.back(result: true);
                }else{
                  Get.back();
                }
              },
              child: Image.asset(
                'static/images/back.png',
                width: 18,
                fit: BoxFit.fitWidth,
              ))),
    ]));
  }
}
